package com.mew.chess.core.game.shbz.game.task;

import cn.hutool.core.util.RandomUtil;
import com.mew.chess.core.common.handler.ChessPushMessageUtil;
import com.mew.chess.core.game.shbz.game.ShbzGameManager;
import com.mew.chess.core.game.shbz.game.ShbzTableManager;
import com.mew.chess.core.game.shbz.game.common.FishBO;
import com.mew.chess.core.game.shbz.game.common.FishConstantValue;
import com.mew.chess.core.game.shbz.game.common.FishLinkType;
import com.mew.chess.core.game.shbz.game.common.Route;
import com.mew.chess.core.game.shbz.game.message.FishCMDMapType;
import com.mew.chess.core.game.shbz.game.message.FishPushMessage;
import com.mew.chess.core.game.shbz.service.ShbzService;
import com.mew.common.rest.bo.GameSession;
import com.mew.common.util.NumberUtil;
import com.zebra.common.concurrent.ZebraScheduler;

import com.zebra.remoting.util.RemotingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.ScheduledFuture;

@Slf4j
@Component
public class FishPushCycleScheduleTask {

    @Autowired
    private ShbzService shbzService;


    public void fishCycle(int roomId, int table,ScheduledFuture<?> stepFuture){

        ShbzTableManager tableManager = ShbzGameManager.getInstance().getController(roomId).getTable(table);
        if (tableManager.getLinkType()!= FishLinkType.Fish.code()){
            if (null != stepFuture){
                ZebraScheduler.cancelScheduled(stepFuture);
            }
            return;
        }

        tableManager.deadFish();

        FishBO[] fishBOs = new FishBO[1];
        if (tableManager.bossTime()){
            //boss
            fishBOs[0] = creatBoss(tableManager);

            //日志
            shbzService.tableLog( tableManager);
        }else {
            //普通
            int rand = NumberUtil.randomIntByStr(FishConstantValue.fishRedisValue.fish_number, "-");
            List<Route> fishRoute = ShbzGameManager.getInstance().getFishRoute();
            HashSet<Integer> ids = new HashSet<>();
            do {
                ids.add(RandomUtil.randomInt(fishRoute.size()));
            }while (ids.size()<rand);

            fishBOs = creatFish(tableManager,ids);

        }

        sendStartMsg(tableManager,fishBOs);
    }

    private FishBO[] creatFish(ShbzTableManager tableManager,HashSet<Integer> ids) {
        List<Route> fishRoute = ShbzGameManager.getInstance().getFishRoute();

        ArrayList<FishBO> fishBOS = new ArrayList<>();
        for (Integer id : ids) {
            Route route = fishRoute.get(id);
            FishBO fishBO = FishBO.creat(tableManager.getFishKeyId().getAndIncrement(), route);
            tableManager.getFishMap().put(fishBO.getFishId(),fishBO);
            fishBOS.add(fishBO);
        }

        return fishBOS.toArray(new FishBO[fishBOS.size()]);
    }

    private FishBO creatBoss(ShbzTableManager tableManager) {
        List<Route> fishRoute = ShbzGameManager.getInstance().getFishBossRoute();
        Route route = fishRoute.get(RandomUtil.randomInt(fishRoute.size()));
        FishBO fishBO = FishBO.creat(tableManager.getFishKeyId().getAndIncrement(), route);
        tableManager.getFishMap().put(fishBO.getFishId(),fishBO);
        return fishBO;
    }

    private void sendStartMsg(ShbzTableManager tableManager, FishBO[] fishBOs){
        FishPushMessage message = FishPushMessage.builder().fishBOs(fishBOs).build();
        tableManager.getOnlineUserMap().forEach((k, v) -> {
            GameSession session = v.getGameSession();
            ChessPushMessageUtil.pushMessage(session,FishCMDMapType.FishPushMessage.code(), message);

        });
    }

}
